/**
  ******************************************************************************
  * Copyright(c) 2018-2023Vincent All rights reserved. 
  * - Filename:    
  * - Author:    Vincent
  * - Version:   V1.0.0
  * - Date:      2019/4/16
  * - Brief:   
  * - FunctionList:
  ******************************************************************************
  * History:
  *   
  *
  * 
  ******************************************************************************
*/
#ifndef __DBG_H__
#define __DBG_H__
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

/* prama define discrible -----------------------------------------------------*/
#ifdef NDEBUG
#define debug(M, ...)

#elif DEBUG_RECODE
  #define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

  #define clean_errno() (errno == 0 ? "None" : strerror(errno))

  #define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

  #define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

  #define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

  #define check(A, M, ...)                           \
    if (!(A))                                        \
    {                                                \
      log_err(M "\n", ##__VA_ARGS__); /* exit(1); */ \
    }

  #define check_exit(A, M, ...)       \
    if (!(A))                         \
    {                                 \
      log_err(M "\n", ##__VA_ARGS__); \
      exit(1);                        \
    }

  #define check_debug(A, M, ...)                   \
    if (!(A))                                      \
    {                                              \
      debug(M "\n", ##__VA_ARGS__); /* exit(1); */ \
    }

#else

  #define debug(M, ...) printf("DEBUG%s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
  #define clean_errno() (errno == 0 ? "None" : strerror(errno))

  #define log_err(M, ...) printf("[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

  #define log_warn(M, ...) printf("[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

  #define log_info(M, ...) printf("[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

  #define check(A, M, ...)                           \
    if (!(A))                                        \
    {                                                \
      log_err(M "\n", ##__VA_ARGS__); /* exit(1); */ \
    }

  #define check_exit(A, M, ...)       \
    if (!(A))                         \
    {                                 \
      log_err(M "\n", ##__VA_ARGS__); \
      exit(1);                        \
    }

  #define check_debug(A, M, ...)                   \
    if (!(A))                                      \
    {                                              \
      debug(M "\n", ##__VA_ARGS__); /* exit(1); */ \
    }
#endif

/* strcuct  discrible --------------------------------------------------------*/

/* prama  discrible ---------------------------------------------------------*/

/* private fucntion  declaration --------------------------------------------*/

/* private fucntion  discrible -----------------------------------------------*/
#endif /*__DBG_H__*/
/************************ (.h) END OF FILE ************************************/
